package com.lysj.bhp.coinexchange.service;

import com.lysj.bhp.coinexchange.base.CoinExchangeContent;
import com.lysj.bhp.coinexchange.domain.Apply;
import com.lysj.bhp.coinexchange.domain.BhpcRecord;
import com.lysj.bhp.user.domain.User;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import com.lysj.bhp.wallet.domain.BhpcWallet;
import com.lysj.bhp.wallet.domain.BitCoinWallet;
import com.lysj.bhp.wallet.domain.EthereumWallet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;

@Service
@Transactional
public class ApplyService extends CoinExchangeContent {

    private static Logger logger = LoggerFactory.getLogger(Apply.class);


    public Resp buyBhpcByUser(BhpcRecord model, int type, String password) throws Exception{
        if (ParamUtil.isBlack(password)) {
            return new Resp(Resp.PARAM_ERROR, "请输入交易密码");
        }
        if (!ParamUtil.md5(password).equals(currentUser().getTradePassword())) {
            return new Resp(Resp.PARAM_ERROR, "交易密码错误");
        }
        if (model.getExchangeNum().compareTo(BigDecimal.ZERO) !=1) {
            return new Resp(Resp.PARAM_ERROR, "金额输入不能小于0");
        }
        //获得用户所有钱包
        BitCoinWallet bitCoinWallet = bitcoinWalletRepository.findByUserId(currentUser().getUserId());
        EthereumWallet ethereumWallet = ethereumWalletRepository.findByUserId(currentUser().getUserId());
        BhpcWallet bhpcWallet = bhpcWalletRepository.findByUserId(currentUser().getUserId());
        //如果用户没创建bhpc钱包,则创建bhpc钱包

        if(ParamUtil.isBlack(bhpcWallet)){
            walletService.myBhpcWallet();
            bhpcWallet = bhpcWalletRepository.findByUserId(currentUser().getUserId());
        }

        BhpcRecord bhpcRecord = new BhpcRecord();
        //用户用比特币钱包兑换BHPC币
        if (type == 10) {
            if(ParamUtil.isBlack(bitCoinWallet)){
                return new Resp(Resp.PARAM_ERROR,"金额不足,请先充值");
            }
            if (model.getExchangeNum().compareTo(bitCoinWallet.getAvailBalance()) == 1) {
                return new Resp(Resp.PARAM_ERROR, "金额不足");
            }
            bhpcRecord.setExchangeRate(exchangeRate.getBITCOIN_TO_DOLLAR());
            bitCoinWallet.setBalance(bitCoinWallet.getBalance().subtract(model.getExchangeNum()));
            bitCoinWallet.setAvailBalance(bitCoinWallet.getAvailBalance().subtract(model.getExchangeNum()));
            bitcoinWalletRepository.save(bitCoinWallet);
            bhpcRecord.setType(10);
        }
        //用户用以太坊钱包兑换BHPC币
        else if (type == 20) {
            if(ParamUtil.isBlack(ethereumWallet)){
                return new Resp(Resp.PARAM_ERROR,"金额不足,请先充值");
            }
            if (model.getExchangeNum().compareTo(ethereumWallet.getAvailBalance()) == 1) {
                return new Resp(Resp.PARAM_ERROR, "金额不足");
            }
            bhpcRecord.setExchangeRate(exchangeRate.getETHEREUM_TO_DOLLAR());
            ethereumWallet.setBalance(ethereumWallet.getBalance().subtract(model.getExchangeNum()));
            ethereumWallet.setAvailBalance(ethereumWallet.getAvailBalance().subtract(model.getExchangeNum()));
            ethereumWalletRepository.save(ethereumWallet);
            bhpcRecord.setType(20);
        } else {
            return new Resp(Resp.PARAM_ERROR, "币种类型错误");
        }
        bhpcRecord.setBhpcToDollar(exchangeRate.getBHPC_TO_DOLLAR());
        bhpcRecord.setCreateDate(new Date());
        bhpcRecord.setUserId(currentUser().getUserId());

        bhpcRecord.setWhitelist(0);

        bhpcRecord.setExchangeNum(model.getExchangeNum());
        BigDecimal dollar = model.getExchangeNum().multiply(bhpcRecord.getExchangeRate()).setScale(6, BigDecimal.ROUND_HALF_UP);
        BigDecimal bigDecimal = dollar.divide(exchangeRate.getBHPC_TO_DOLLAR(),6, BigDecimal.ROUND_HALF_UP);

        bhpcRecord.setBhpc(bigDecimal); //通过计算获得Bhpc的数量
        bhpcRecord.setStatus("成功");
        bhpcWallet.setBalance(bhpcWallet.getBalance().add(bigDecimal));
        bhpcWallet.setAvailBalance(bhpcWallet.getAvailBalance().add(bigDecimal));
        bhpcWalletRepository.save(bhpcWallet);
        bhpcRecordRepository.save(bhpcRecord);
        logger.info(currentUser().getEmail() + "->购买了:" + bigDecimal + " 个算力币!!");
        return new Resp("购买成功");
    }

}
